/*
 * Copyright 2008-2009 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public Licence Version 3
 * The licence can be read in its entirety in the LICENSE.txt file accompaning this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of WeightWatch.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.cachapa.weightwatch.ui;

import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import net.cachapa.weightwatch.Value;
import net.cachapa.weightwatch.ui.components.DateField;
import net.cachapa.weightwatch.ui.components.NumericField;

public class InsertForm extends Form implements ActionListener {
	private Application application;
	private DateField dateField;
	private NumericField weightField;
	private Command acceptCommand;
	private Command cancelCommand;
	
	public InsertForm(Application application) {
		this.application = application;
		this.setTitle("insert_title");
		this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		
		// Date Field
		Container dateContainer = new Container(new BoxLayout(BoxLayout.X_AXIS));
		dateContainer.addComponent(new Label("insert_date"));
		this.dateField = new DateField(this);
		dateContainer.addComponent(dateField);
		this.addComponent(dateContainer);
		
		// Weight
		Container weightContainer = new Container(new BoxLayout(BoxLayout.X_AXIS));
		weightContainer.addComponent(new Label("insert_weight"));
		weightField = new NumericField();
		weightField.setUpDownValueChange(0.1);
		weightField.setValue(application.getWeightWatch().getDatabase().getLatestValue().weight);
		weightContainer.addComponent(weightField);
		this.addComponent(weightContainer);
		
		acceptCommand = new Command("accept");
		cancelCommand = new Command("cancel");
		this.addCommand(acceptCommand);
		this.addCommand(cancelCommand);
		this.setBackCommand(cancelCommand);
		this.setCommandListener(this);
	}
	
	public void show() {
		Value latestValue = application.getWeightWatch().getDatabase().getLatestValue();
		weightField.setValue(latestValue.weight);
		this.setFocused(weightField);
		super.show();
	}

	public void actionPerformed(ActionEvent ae) {
		Object source = ae.getSource();
		if (source == acceptCommand) {
			boolean result = application.getWeightWatch().addValue(dateField.getTime(), weightField.getValue());
			if (!result) {
				Dialog.show("error", "insert_dateexists",
					new Command[] {new Command("ok")}, Dialog.TYPE_ERROR, null, 0);
				return;
			}
			application.performAction(Application.SHOW_MAIN_FORM);
		}
		else if (source == cancelCommand) {
			application.performAction(Application.SHOW_MAIN_FORM);
		}
	}
}
